home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / inspect.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  23.7 KB  |  698 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. __author__ = 'Ka-Ping Yee <ping@lfw.org>'
  5. __date__ = '1 Jan 2001'
  6. import sys
  7. import os
  8. import types
  9. import string
  10. import re
  11. import dis
  12. import imp
  13. import tokenize
  14. import linecache
  15.  
  16. def ismodule(object):
  17.     return isinstance(object, types.ModuleType)
  18.  
  19.  
  20. def isclass(object):
  21.     if not isinstance(object, types.ClassType):
  22.         pass
  23.     return hasattr(object, '__bases__')
  24.  
  25.  
  26. def ismethod(object):
  27.     return isinstance(object, types.MethodType)
  28.  
  29.  
  30. def ismethoddescriptor(object):
  31.     if hasattr(object, '__get__') and not hasattr(object, '__set__') and not ismethod(object) and not isfunction(object):
  32.         pass
  33.     return not isclass(object)
  34.  
  35.  
  36. def isdatadescriptor(object):
  37.     if hasattr(object, '__set__'):
  38.         pass
  39.     return hasattr(object, '__get__')
  40.  
  41.  
  42. def isfunction(object):
  43.     return isinstance(object, types.FunctionType)
  44.  
  45.  
  46. def istraceback(object):
  47.     return isinstance(object, types.TracebackType)
  48.  
  49.  
  50. def isframe(object):
  51.     return isinstance(object, types.FrameType)
  52.  
  53.  
  54. def iscode(object):
  55.     return isinstance(object, types.CodeType)
  56.  
  57.  
  58. def isbuiltin(object):
  59.     return isinstance(object, types.BuiltinFunctionType)
  60.  
  61.  
  62. def isroutine(object):
  63.     if not isbuiltin(object) and isfunction(object) and ismethod(object):
  64.         pass
  65.     return ismethoddescriptor(object)
  66.  
  67.  
  68. def getmembers(object, predicate = None):
  69.     results = []
  70.     for key in dir(object):
  71.         value = getattr(object, key)
  72.         if not predicate or predicate(value):
  73.             results.append((key, value))
  74.             continue
  75.     
  76.     results.sort()
  77.     return results
  78.  
  79.  
  80. def classify_class_attrs(cls):
  81.     mro = getmro(cls)
  82.     names = dir(cls)
  83.     result = []
  84.     for name in names:
  85.         if name in cls.__dict__:
  86.             obj = cls.__dict__[name]
  87.         else:
  88.             obj = getattr(cls, name)
  89.         homecls = getattr(obj, '__objclass__', None)
  90.         if homecls is None:
  91.             for base in mro:
  92.                 if name in base.__dict__:
  93.                     homecls = base
  94.                     break
  95.                     continue
  96.             
  97.         
  98.         if homecls is not None and name in homecls.__dict__:
  99.             obj = homecls.__dict__[name]
  100.         
  101.         obj_via_getattr = getattr(cls, name)
  102.         if isinstance(obj, staticmethod):
  103.             kind = 'static method'
  104.         elif isinstance(obj, classmethod):
  105.             kind = 'class method'
  106.         elif isinstance(obj, property):
  107.             kind = 'property'
  108.         elif ismethod(obj_via_getattr) or ismethoddescriptor(obj_via_getattr):
  109.             kind = 'method'
  110.         else:
  111.             kind = 'data'
  112.         result.append((name, kind, homecls, obj))
  113.     
  114.     return result
  115.  
  116.  
  117. def _searchbases(cls, accum):
  118.     if cls in accum:
  119.         return None
  120.     
  121.     accum.append(cls)
  122.     for base in cls.__bases__:
  123.         _searchbases(base, accum)
  124.     
  125.  
  126.  
  127. def getmro(cls):
  128.     if hasattr(cls, '__mro__'):
  129.         return cls.__mro__
  130.     else:
  131.         result = []
  132.         _searchbases(cls, result)
  133.         return tuple(result)
  134.  
  135.  
  136. def indentsize(line):
  137.     expline = string.expandtabs(line)
  138.     return len(expline) - len(string.lstrip(expline))
  139.  
  140.  
  141. def getdoc(object):
  142.     
  143.     try:
  144.         doc = object.__doc__
  145.     except AttributeError:
  146.         return None
  147.  
  148.     if not isinstance(doc, types.StringTypes):
  149.         return None
  150.     
  151.     
  152.     try:
  153.         lines = string.split(string.expandtabs(doc), '\n')
  154.     except UnicodeError:
  155.         return None
  156.  
  157.     margin = sys.maxint
  158.     for line in lines[1:]:
  159.         content = len(string.lstrip(line))
  160.         if content:
  161.             indent = len(line) - content
  162.             margin = min(margin, indent)
  163.             continue
  164.     
  165.     if lines:
  166.         lines[0] = lines[0].lstrip()
  167.     
  168.     if margin < sys.maxint:
  169.         for i in range(1, len(lines)):
  170.             lines[i] = lines[i][margin:]
  171.         
  172.     
  173.     while lines and not lines[-1]:
  174.         lines.pop()
  175.     while lines and not lines[0]:
  176.         lines.pop(0)
  177.     return string.join(lines, '\n')
  178.  
  179.  
  180. def getfile(object):
  181.     if ismodule(object):
  182.         if hasattr(object, '__file__'):
  183.             return object.__file__
  184.         
  185.         raise TypeError('arg is a built-in module')
  186.     
  187.     if isclass(object):
  188.         object = sys.modules.get(object.__module__)
  189.         if hasattr(object, '__file__'):
  190.             return object.__file__
  191.         
  192.         raise TypeError('arg is a built-in class')
  193.     
  194.     if ismethod(object):
  195.         object = object.im_func
  196.     
  197.     if isfunction(object):
  198.         object = object.func_code
  199.     
  200.     if istraceback(object):
  201.         object = object.tb_frame
  202.     
  203.     if isframe(object):
  204.         object = object.f_code
  205.     
  206.     if iscode(object):
  207.         return object.co_filename
  208.     
  209.     raise TypeError('arg is not a module, class, method, function, traceback, frame, or code object')
  210.  
  211.  
  212. def getmoduleinfo(path):
  213.     filename = os.path.basename(path)
  214.     suffixes = map((lambda .0: (suffix, mode, mtype) = .0(-len(suffix), suffix, mode, mtype)), imp.get_suffixes())
  215.     suffixes.sort()
  216.     for neglen, suffix, mode, mtype in suffixes:
  217.         if filename[neglen:] == suffix:
  218.             return (filename[:neglen], suffix, mode, mtype)
  219.             continue
  220.     
  221.  
  222.  
  223. def getmodulename(path):
  224.     info = getmoduleinfo(path)
  225.     if info:
  226.         return info[0]
  227.     
  228.  
  229.  
  230. def getsourcefile(object):
  231.     filename = getfile(object)
  232.     if string.lower(filename[-4:]) in [
  233.         '.pyc',
  234.         '.pyo']:
  235.         filename = filename[:-4] + '.py'
  236.     
  237.     for suffix, mode, kind in imp.get_suffixes():
  238.         if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
  239.             return None
  240.             continue
  241.     
  242.     if os.path.exists(filename):
  243.         return filename
  244.     
  245.  
  246.  
  247. def getabsfile(object):
  248.     if not getsourcefile(object):
  249.         pass
  250.     return os.path.normcase(os.path.abspath(getfile(object)))
  251.  
  252. modulesbyfile = { }
  253.  
  254. def getmodule(object):
  255.     if ismodule(object):
  256.         return object
  257.     
  258.     if isclass(object):
  259.         return sys.modules.get(object.__module__)
  260.     
  261.     
  262.     try:
  263.         file = getabsfile(object)
  264.     except TypeError:
  265.         return None
  266.  
  267.     if file in modulesbyfile:
  268.         return sys.modules.get(modulesbyfile[file])
  269.     
  270.     for module in sys.modules.values():
  271.         if hasattr(module, '__file__'):
  272.             modulesbyfile[getabsfile(module)] = module.__name__
  273.             continue
  274.     
  275.     if file in modulesbyfile:
  276.         return sys.modules.get(modulesbyfile[file])
  277.     
  278.     main = sys.modules['__main__']
  279.     if not hasattr(object, '__name__'):
  280.         return None
  281.     
  282.     if hasattr(main, object.__name__):
  283.         mainobject = getattr(main, object.__name__)
  284.         if mainobject is object:
  285.             return main
  286.         
  287.     
  288.     builtin = sys.modules['__builtin__']
  289.     if hasattr(builtin, object.__name__):
  290.         builtinobject = getattr(builtin, object.__name__)
  291.         if builtinobject is object:
  292.             return builtin
  293.         
  294.     
  295.  
  296.  
  297. def findsource(object):
  298.     if not getsourcefile(object):
  299.         pass
  300.     file = getfile(object)
  301.     lines = linecache.getlines(file)
  302.     if not lines:
  303.         raise IOError('could not get source code')
  304.     
  305.     if ismodule(object):
  306.         return (lines, 0)
  307.     
  308.     if isclass(object):
  309.         name = object.__name__
  310.         pat = re.compile('^\\s*class\\s*' + name + '\\b')
  311.         for i in range(len(lines)):
  312.             if pat.match(lines[i]):
  313.                 return (lines, i)
  314.                 continue
  315.         else:
  316.             raise IOError('could not find class definition')
  317.     
  318.     if ismethod(object):
  319.         object = object.im_func
  320.     
  321.     if isfunction(object):
  322.         object = object.func_code
  323.     
  324.     if istraceback(object):
  325.         object = object.tb_frame
  326.     
  327.     if isframe(object):
  328.         object = object.f_code
  329.     
  330.     if iscode(object):
  331.         if not hasattr(object, 'co_firstlineno'):
  332.             raise IOError('could not find function definition')
  333.         
  334.         lnum = object.co_firstlineno - 1
  335.         pat = re.compile('^(\\s*def\\s)|(.*\\slambda(:|\\s))')
  336.         while lnum > 0:
  337.             if pat.match(lines[lnum]):
  338.                 break
  339.             
  340.             lnum = lnum - 1
  341.         return (lines, lnum)
  342.     
  343.     raise IOError('could not find code object')
  344.  
  345.  
  346. def getcomments(object):
  347.     
  348.     try:
  349.         (lines, lnum) = findsource(object)
  350.     except (IOError, TypeError):
  351.         return None
  352.  
  353.     if ismodule(object):
  354.         start = 0
  355.         if lines and lines[0][:2] == '#!':
  356.             start = 1
  357.         
  358.         while start < len(lines) and string.strip(lines[start]) in [
  359.             '',
  360.             '#']:
  361.             start = start + 1
  362.         if start < len(lines) and lines[start][:1] == '#':
  363.             comments = []
  364.             end = start
  365.             while end < len(lines) and lines[end][:1] == '#':
  366.                 comments.append(string.expandtabs(lines[end]))
  367.                 end = end + 1
  368.             return string.join(comments, '')
  369.         
  370.     elif lnum > 0:
  371.         indent = indentsize(lines[lnum])
  372.         end = lnum - 1
  373.         if end >= 0 and string.lstrip(lines[end])[:1] == '#' and indentsize(lines[end]) == indent:
  374.             comments = [
  375.                 string.lstrip(string.expandtabs(lines[end]))]
  376.             if end > 0:
  377.                 end = end - 1
  378.                 comment = string.lstrip(string.expandtabs(lines[end]))
  379.                 while comment[:1] == '#' and indentsize(lines[end]) == indent:
  380.                     comments[:0] = [
  381.                         comment]
  382.                     end = end - 1
  383.                     if end < 0:
  384.                         break
  385.                     
  386.                     comment = string.lstrip(string.expandtabs(lines[end]))
  387.             
  388.             while comments and string.strip(comments[0]) == '#':
  389.                 comments[:1] = []
  390.             while comments and string.strip(comments[-1]) == '#':
  391.                 comments[-1:] = []
  392.             return string.join(comments, '')
  393.         
  394.     
  395.  
  396.  
  397. class ListReader:
  398.     
  399.     def __init__(self, lines):
  400.         self.lines = lines
  401.         self.index = 0
  402.  
  403.     
  404.     def readline(self):
  405.         i = self.index
  406.         if i < len(self.lines):
  407.             self.index = i + 1
  408.             return self.lines[i]
  409.         else:
  410.             return ''
  411.  
  412.  
  413.  
  414. class EndOfBlock(Exception):
  415.     pass
  416.  
  417.  
  418. class BlockFinder:
  419.     
  420.     def __init__(self):
  421.         self.indent = 0
  422.         self.started = 0
  423.         self.last = 0
  424.  
  425.     
  426.     def tokeneater(self, type, token, .6, .8, line):
  427.         (srow, scol) = .6
  428.         (erow, ecol) = .8
  429.         if not (self.started):
  430.             if type == tokenize.NAME:
  431.                 self.started = 1
  432.             
  433.         elif type == tokenize.NEWLINE:
  434.             self.last = srow
  435.         elif type == tokenize.INDENT:
  436.             self.indent = self.indent + 1
  437.         elif type == tokenize.DEDENT:
  438.             self.indent = self.indent - 1
  439.             if self.indent == 0:
  440.                 raise EndOfBlock, self.last
  441.             
  442.         elif type == tokenize.NAME and scol == 0:
  443.             raise EndOfBlock, self.last
  444.         
  445.  
  446.  
  447.  
  448. def getblock(lines):
  449.     
  450.     try:
  451.         tokenize.tokenize(ListReader(lines).readline, BlockFinder().tokeneater)
  452.     except EndOfBlock:
  453.         eob = None
  454.         return lines[:eob.args[0]]
  455.  
  456.     return lines[:1]
  457.  
  458.  
  459. def getsourcelines(object):
  460.     (lines, lnum) = findsource(object)
  461.     if ismodule(object):
  462.         return (lines, 0)
  463.     else:
  464.         return (getblock(lines[lnum:]), lnum + 1)
  465.  
  466.  
  467. def getsource(object):
  468.     (lines, lnum) = getsourcelines(object)
  469.     return string.join(lines, '')
  470.  
  471.  
  472. def walktree(classes, children, parent):
  473.     results = []
  474.     classes.sort((lambda a, b: cmp(a.__name__, b.__name__)))
  475.     for c in classes:
  476.         results.append((c, c.__bases__))
  477.         if c in children:
  478.             results.append(walktree(children[c], children, c))
  479.             continue
  480.     
  481.     return results
  482.  
  483.  
  484. def getclasstree(classes, unique = 0):
  485.     children = { }
  486.     roots = []
  487.     for c in classes:
  488.         if c.__bases__:
  489.             for parent in c.__bases__:
  490.                 if not (parent in children):
  491.                     children[parent] = []
  492.                 
  493.                 children[parent].append(c)
  494.                 if unique and parent in classes:
  495.                     break
  496.                     continue
  497.             
  498.         if c not in roots:
  499.             roots.append(c)
  500.             continue
  501.     
  502.     for parent in children:
  503.         if parent not in classes:
  504.             roots.append(parent)
  505.             continue
  506.     
  507.     return walktree(roots, children, None)
  508.  
  509. (CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS) = (1, 2, 4, 8)
  510.  
  511. def getargs(co):
  512.     if not iscode(co):
  513.         raise TypeError('arg is not a code object')
  514.     
  515.     code = co.co_code
  516.     nargs = co.co_argcount
  517.     names = co.co_varnames
  518.     args = list(names[:nargs])
  519.     step = 0
  520.     for i in range(nargs):
  521.         if args[i][:1] in [
  522.             '',
  523.             '.']:
  524.             (stack, remain, count) = ([], [], [])
  525.             while step < len(code):
  526.                 op = ord(code[step])
  527.                 step = step + 1
  528.                 if op >= dis.HAVE_ARGUMENT:
  529.                     opname = dis.opname[op]
  530.                     value = ord(code[step]) + ord(code[step + 1]) * 256
  531.                     step = step + 2
  532.                     if opname in [
  533.                         'UNPACK_TUPLE',
  534.                         'UNPACK_SEQUENCE']:
  535.                         remain.append(value)
  536.                         count.append(value)
  537.                     elif opname == 'STORE_FAST':
  538.                         stack.append(names[value])
  539.                         remain[-1] = remain[-1] - 1
  540.                         while remain[-1] == 0:
  541.                             remain.pop()
  542.                             size = count.pop()
  543.                             stack[-size:] = [
  544.                                 stack[-size:]]
  545.                             if not remain:
  546.                                 break
  547.                             
  548.                             remain[-1] = remain[-1] - 1
  549.                         if not remain:
  550.                             break
  551.                         
  552.                     
  553.                 opname in [
  554.                     'UNPACK_TUPLE',
  555.                     'UNPACK_SEQUENCE']
  556.             args[i] = stack[0]
  557.             continue
  558.     
  559.     varargs = None
  560.     if co.co_flags & CO_VARARGS:
  561.         varargs = co.co_varnames[nargs]
  562.         nargs = nargs + 1
  563.     
  564.     varkw = None
  565.     if co.co_flags & CO_VARKEYWORDS:
  566.         varkw = co.co_varnames[nargs]
  567.     
  568.     return (args, varargs, varkw)
  569.  
  570.  
  571. def getargspec(func):
  572.     if ismethod(func):
  573.         func = func.im_func
  574.     
  575.     if not isfunction(func):
  576.         raise TypeError('arg is not a Python function')
  577.     
  578.     (args, varargs, varkw) = getargs(func.func_code)
  579.     return (args, varargs, varkw, func.func_defaults)
  580.  
  581.  
  582. def getargvalues(frame):
  583.     (args, varargs, varkw) = getargs(frame.f_code)
  584.     return (args, varargs, varkw, frame.f_locals)
  585.  
  586.  
  587. def joinseq(seq):
  588.     if len(seq) == 1:
  589.         return '(' + seq[0] + ',)'
  590.     else:
  591.         return '(' + string.join(seq, ', ') + ')'
  592.  
  593.  
  594. def strseq(object, convert, join = joinseq):
  595.     if type(object) in [
  596.         types.ListType,
  597.         types.TupleType]:
  598.         return join(map((lambda o, c = convert, j = join: strseq(o, c, j)), object))
  599.     else:
  600.         return convert(object)
  601.  
  602.  
  603. def formatargspec(args, varargs = None, varkw = None, defaults = None, formatarg = str, formatvarargs = (lambda name: '*' + name), formatvarkw = (lambda name: '**' + name), formatvalue = (lambda value: '=' + repr(value)), join = joinseq):
  604.     specs = []
  605.     if defaults:
  606.         firstdefault = len(args) - len(defaults)
  607.     
  608.     for i in range(len(args)):
  609.         spec = strseq(args[i], formatarg, join)
  610.         if defaults and i >= firstdefault:
  611.             spec = spec + formatvalue(defaults[i - firstdefault])
  612.         
  613.         specs.append(spec)
  614.     
  615.     if varargs is not None:
  616.         specs.append(formatvarargs(varargs))
  617.     
  618.     if varkw is not None:
  619.         specs.append(formatvarkw(varkw))
  620.     
  621.     return '(' + string.join(specs, ', ') + ')'
  622.  
  623.  
  624. def formatargvalues(args, varargs, varkw, locals, formatarg = str, formatvarargs = (lambda name: '*' + name), formatvarkw = (lambda name: '**' + name), formatvalue = (lambda value: '=' + repr(value)), join = joinseq):
  625.     
  626.     def convert(name, locals = locals, formatarg = formatarg, formatvalue = formatvalue):
  627.         return formatarg(name) + formatvalue(locals[name])
  628.  
  629.     specs = []
  630.     for i in range(len(args)):
  631.         specs.append(strseq(args[i], convert, join))
  632.     
  633.     if varargs:
  634.         specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
  635.     
  636.     if varkw:
  637.         specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
  638.     
  639.     return '(' + string.join(specs, ', ') + ')'
  640.  
  641.  
  642. def getframeinfo(frame, context = 1):
  643.     if istraceback(frame):
  644.         frame = frame.tb_frame
  645.     
  646.     if not isframe(frame):
  647.         raise TypeError('arg is not a frame or traceback object')
  648.     
  649.     if not getsourcefile(frame):
  650.         pass
  651.     filename = getfile(frame)
  652.     lineno = frame.f_lineno
  653.     if context > 0:
  654.         start = lineno - 1 - context // 2
  655.         
  656.         try:
  657.             (lines, lnum) = findsource(frame)
  658.         except IOError:
  659.             lines = index = None
  660.  
  661.         start = max(start, 1)
  662.         start = min(start, len(lines) - context)
  663.         lines = lines[start:start + context]
  664.         index = lineno - 1 - start
  665.     else:
  666.         lines = index = None
  667.     return (filename, lineno, frame.f_code.co_name, lines, index)
  668.  
  669.  
  670. def getlineno(frame):
  671.     return frame.f_lineno
  672.  
  673.  
  674. def getouterframes(frame, context = 1):
  675.     framelist = []
  676.     while frame:
  677.         framelist.append((frame,) + getframeinfo(frame, context))
  678.         frame = frame.f_back
  679.     return framelist
  680.  
  681.  
  682. def getinnerframes(tb, context = 1):
  683.     framelist = []
  684.     while tb:
  685.         framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
  686.         tb = tb.tb_next
  687.     return framelist
  688.  
  689. currentframe = sys._getframe
  690.  
  691. def stack(context = 1):
  692.     return getouterframes(sys._getframe(1), context)
  693.  
  694.  
  695. def trace(context = 1):
  696.     return getinnerframes(sys.exc_info()[2], context)
  697.  
  698.